/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lojavirtual.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lojavirtual.model.Role;
import lojavirtual.model.Usuario;
import lojavirtual.persistence.RoleDAO;
import lojavirtual.persistence.UsuarioDAO;

/**
 *
 * @author 710930
 */
public class ValidarUsuarioServlet extends HttpServlet {
   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String login = request.getParameter("login");
        String senha = request.getParameter("senha");

        Role role = new Role();

        role = logarUsuario(login, senha);


            request.getSession().setAttribute("role", role.getRole());
            request.getSession().setAttribute("usuariologin", role.getUsuario().getLogin());
            request.getSession().setAttribute("nomeusuario", role.getUsuario().getNome());
            response.sendRedirect("Home.jsp");
        


    }

public Role logarUsuario(String login, String senha){
    Usuario usuario = new Usuario();
    usuario.setLogin(login);
    usuario.setSenha(senha);

    UsuarioDAO usuarioDAO = new UsuarioDAO();

    Role role = new Role();
    if(usuarioDAO.logar(usuario)){
        
        role = acesso(usuario);
    }

    return role;

}

public Role acesso(Usuario usuario){
    Role role = new Role();

    RoleDAO roleDAO = new RoleDAO();
    role = roleDAO.tipoacesso(usuario);

    return role;
}
}
